/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package utfpr.cp.control;

import edu.uci.ics.crawler4j.url.WebURL;
import java.util.HashSet;
import java.util.List;
import javax.persistence.EntityManager;
import utfpr.cp.model.EntityManagerHelper;
import utfpr.cp.model.URLData;
import utfpr.cp.model.URLDataDAO;

/**
 *
 * @author Adriano_2
 */
public class URLControl {
    
    public static URLData getOrCreate(WebURL url) {
        EntityManager em = EntityManagerHelper.getEntityManager();
        URLDataDAO dao = new URLDataDAO(em);
        return dao.getOrCreate(url);
    }
    
    public static HashSet<URLData> getOrCreateAll(List<WebURL> list) {
        EntityManager em = EntityManagerHelper.getEntityManager();
        URLDataDAO dao = new URLDataDAO(em);
        HashSet<URLData> links = new HashSet<>();
        for (WebURL link : list) {
            links.add(dao.getOrCreate(link));
            /*
            if (!em.isOpen()) {
                em = EntityManagerHelper.getEntityManager();
            }*/
        }
        return links;
    }
}
